#include "details.h"
#include "ui_details.h"

#include <QSqlQuery>
#include <QMessageBox>




Details::Details(QString itemId,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Details)
{
    ui->setupUi(this);
    ui->id_label->setText(itemId);
    QSqlQuery query;
    query.exec("select * from patient");
    QString findStr = QString("select * from patient where ID = \"%1\"").arg(itemId);
    query.exec(findStr);
    while (query.next()) {
        ui->name_lineEdit->setText(query.value(1).toString());
        ui->birthday_dateEdit->setDate(QDate::fromString(query.value(7).toString(),"yyyy/MM/dd"));
        ui->damage_dateEdit->setDate(QDate::fromString(query.value(8).toString(),"yyyy/MM/dd"));
        ui->surgeryDate_dateEdit->setDate(QDate::fromString(query.value(9).toString(),"yyyy/MM/dd"));
        ui->assessDate_dateEdit->setDate(QDate::fromString(query.value(10).toString(),"yyyy/MM/dd"));
        ui->job_textEdit->setPlainText(query.value(5).toString());
        ui->diagnose_textEdit->setPlainText(query.value(12).toString());
        ui->history_textEdit->setPlainText(query.value(13).toString());
        QString gender = query.value(2).toString();
        if(gender == "男") ui->maleRadio->setChecked(true);
        else ui->femaleRadio->setChecked(true);
        ui->checkTimes_spinBox->setValue(query.value(6).toInt());
        ui->comboBox->setCurrentText(query.value(11).toString());
        QString good = query.value(3).toString();
        if(good == "左侧")
        {
            ui->left_goodRadio->setChecked(true);
            ui->both_goodRadio->setChecked(false);
            ui->right_goodRadio->setChecked(false);
        }
        else if(good == "右侧")
        {
            ui->left_goodRadio->setChecked(false);
            ui->both_goodRadio->setChecked(false);
            ui->right_goodRadio->setChecked(true);
        }
        else if(good == "双侧")
        {
            ui->left_goodRadio->setChecked(false);
            ui->both_goodRadio->setChecked(true);
            ui->right_goodRadio->setChecked(false);
        }
        else
        {
            ui->left_goodRadio->setChecked(false);
            ui->both_goodRadio->setChecked(false);
            ui->right_goodRadio->setChecked(false);
        }
        QString bad = query.value(4).toString();
        if(bad =="左侧")
        {
            ui->left_badRadio->setChecked(true);
            ui->both_badRadio->setChecked(false);
            ui->right_badRadio->setChecked(false);
        }
        else if(bad == "右侧")
        {
            ui->left_badRadio->setChecked(false);
            ui->both_badRadio->setChecked(false);
            ui->right_badRadio->setChecked(true);
        }
        else if(bad == "双侧")
        {
            ui->left_badRadio->setChecked(false);
            ui->both_badRadio->setChecked(true);
            ui->right_badRadio->setChecked(false);
        }
        else {
            ui->left_badRadio->setChecked(false);
            ui->both_badRadio->setChecked(false);
            ui->right_badRadio->setChecked(false);
        }

    }
    int age = (QDate(QDate::currentDate()).year()) - (ui->birthday_dateEdit->date().year());
    ui->age_label->setText(QString::number(age));
    connect(ui->name_lineEdit,SIGNAL(textChanged(QString)),this,SLOT(slot_BtnEnabled()));
    connect(ui->birthday_dateEdit,SIGNAL(dateChanged(QDate)),this,SLOT(slot_BtnEnabled()));
    connect(ui->damage_dateEdit,SIGNAL(dateChanged(QDate)),this,SLOT(slot_BtnEnabled()));
    connect(ui->surgeryDate_dateEdit,SIGNAL(dateChanged(QDate)),this,SLOT(slot_BtnEnabled()));
    connect(ui->assessDate_dateEdit,SIGNAL(dateChanged(QDate)),this,SLOT(slot_BtnEnabled()));
    connect(ui->job_textEdit,SIGNAL(textChanged()),this,SLOT(slot_BtnEnabled()));
    connect(ui->diagnose_textEdit,SIGNAL(textChanged()),this,SLOT(slot_BtnEnabled()));
    connect(ui->history_textEdit,SIGNAL(textChanged()),this,SLOT(slot_BtnEnabled()));
    connect(ui->left_goodRadio,SIGNAL(clicked(bool)),this,SLOT(slot_BtnEnabled()));
    connect(ui->both_goodRadio,SIGNAL(clicked(bool)),this,SLOT(slot_BtnEnabled()));
    connect(ui->right_goodRadio,SIGNAL(clicked(bool)),this,SLOT(slot_BtnEnabled()));
    connect(ui->left_badRadio,SIGNAL(clicked(bool)),this,SLOT(slot_BtnEnabled()));
    connect(ui->both_badRadio,SIGNAL(clicked(bool)),this,SLOT(slot_BtnEnabled()));
    connect(ui->right_badRadio,SIGNAL(clicked(bool)),this,SLOT(slot_BtnEnabled()));
    connect(ui->comboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(slot_BtnEnabled()));
}

void Details::slot_BtnEnabled()
{
    ui->changedBtn->setEnabled(true);
}

Details::~Details()
{
    delete ui;
}

void Details::on_changedBtn_clicked()
{
    //修改信息
    if(ui->name_lineEdit->text().isEmpty()  || ui->job_textEdit->document()->isEmpty())
        QMessageBox::information(this,"提示","请输入完整信息！");
    else if(ui->diagnose_textEdit->document()->isEmpty() || ui->history_textEdit->document()->isEmpty())
        QMessageBox::information(this,"提示","请填写诊断和病史");
    else {
        QString id = ui->id_label->text();
        QString newName = ui->name_lineEdit->text();
        QString newGender;
        if(ui->maleRadio->isChecked()) newGender = "男";
        else newGender = "女";
        QString newGood;
        if (ui->left_goodRadio->isChecked()) newGood = "左侧";
        else if (ui->right_goodRadio->isChecked()) newGood = "右侧";
        else if (ui->both_goodRadio->isChecked()) newGood = "双侧";
        else newGood = "————";
        QString newBad;
        if (ui->left_badRadio->isChecked()) newBad = "左侧";
        else if (ui->right_badRadio->isChecked()) newBad = "右侧";
        else if (ui->both_badRadio->isChecked()) newGood = "双侧";
        else newBad = "————";
        QString newJob = ui->job_textEdit->document()->toPlainText();
        QString newCheckTimes = ui->checkTimes_spinBox->text();
        QString newBirthday = ui->birthday_dateEdit->date().toString("yyyy/MM/dd");
        QString newDamageDay = ui->damage_dateEdit->date().toString("yyyy/MM/dd");
        QString newSurgeryDay = ui->surgeryDate_dateEdit->date().toString("yyyy/MM/dd");
        QString newAsseessDay = ui->assessDate_dateEdit->date().toString("yyyy/MM/dd");
        QString newDoctor = ui->comboBox->currentText();
        QString newDiagnose = ui->diagnose_textEdit->document()->toPlainText();
        QString newHistory = ui->history_textEdit->document()->toPlainText();
        QString newAge = ui->age_label->text();
        QSqlQuery query;
        query.exec("select * from patient");
        query.next();
        QString updateStr = QString("update patient set NAME = \"%1\",GENGER = \"%2\","
                                    "GOOD = \"%3\",BAD = \"%4\",JOB = \"%5\",CHECKTIMES = \"%6\","
                                    "BIRTHDAY = \"%7\",DAMAGEDAY = \"%8\",SURGERYDAY = \"%9\",ASSEESSDAY = \"%10\","
                                    "DOCTOR = \"%11\",DIAGNOSE = \"%12\",HISTORY = \"%13\" where ID = \"%14\"").
                arg(newName,newGender,newGood,newBad,newJob,newCheckTimes,newBirthday).
                arg(newDamageDay,newSurgeryDay,newAsseessDay,newDoctor,newDiagnose,newHistory,id);
        if(query.exec(updateStr))
        {
            int i = QMessageBox::information(this,"成功","修改成功！","确定");
            if(i == QObject::tr("确定").toInt())
            {
                emit changeRecord();
                this->close();
            }
        }
        else
            QMessageBox::information(this,"提示","修改失败","确定");
    }


}

void Details::on_left_goodRadio_clicked(bool checked)
{
    if(checked == true)
    {
        ui->both_goodRadio->setChecked(false);
        ui->right_goodRadio->setChecked(false);
        ui->right_badRadio->setChecked(true);
        ui->left_badRadio->setChecked(false);
        ui->both_badRadio->setChecked(false);
    }
    else
    {
        ui->right_goodRadio->setChecked(true);
        ui->both_goodRadio->setChecked(false);
        ui->right_badRadio->setChecked(false);
        ui->left_badRadio->setChecked(true);
        ui->both_badRadio->setChecked(false);
    }
}

void Details::on_both_goodRadio_clicked(bool checked)
{
    if(checked == true)
    {
        ui->left_goodRadio->setChecked(false);
        ui->right_goodRadio->setChecked(false);
        ui->right_badRadio->setChecked(false);
        ui->left_badRadio->setChecked(false);
        ui->both_badRadio->setChecked(false);
    }
    else
    {
        ui->left_goodRadio->setChecked(true);
        ui->right_goodRadio->setChecked(false);
        ui->right_badRadio->setChecked(true);
        ui->left_badRadio->setChecked(false);
        ui->both_badRadio->setChecked(false);
    }
}

void Details::on_right_goodRadio_clicked(bool checked)
{
    if(checked == true)
    {
        ui->left_goodRadio->setChecked(false);
        ui->both_goodRadio->setChecked(false);
        ui->right_badRadio->setChecked(false);
        ui->left_badRadio->setChecked(true);
        ui->both_badRadio->setChecked(false);
    }
    else
    {
        ui->left_goodRadio->setChecked(true);
        ui->both_goodRadio->setChecked(false);
        ui->right_badRadio->setChecked(true);
        ui->left_badRadio->setChecked(false);
        ui->both_badRadio->setChecked(false);
    }
}

void Details::on_left_badRadio_clicked(bool checked)
{
    if(checked == true)
    {
        ui->both_badRadio->setChecked(false);
        ui->right_badRadio->setChecked(false);
        ui->right_goodRadio->setChecked(true);
        ui->left_goodRadio->setChecked(false);
        ui->both_goodRadio->setChecked(false);
    }
    else
    {
        ui->right_badRadio->setChecked(true);
        ui->both_badRadio->setChecked(false);
        ui->left_goodRadio->setCheckable(true);
        ui->both_goodRadio->setCheckable(false);
        ui->right_goodRadio->setCheckable(false);
    }
}

void Details::on_both_badRadio_clicked(bool checked)
{
    if( checked == true)
    {
        ui->left_goodRadio->setChecked(false);
        ui->both_goodRadio->setChecked(false);
        ui->right_goodRadio->setChecked(false);
        ui->left_badRadio->setChecked(false);
        ui->right_badRadio->setChecked(false);
    }
    else
    {
        ui->left_badRadio->setChecked(true);
        ui->right_badRadio->setChecked(false);
        ui->left_goodRadio->setChecked(false);
        ui->both_goodRadio->setChecked(false);
        ui->right_goodRadio->setChecked(true);
    }
}

void Details::on_right_badRadio_clicked(bool checked)
{
    if(checked == true)
    {
        ui->left_badRadio->setChecked(false);
        ui->both_badRadio->setChecked(false);
        ui->left_goodRadio->setChecked(true);
        ui->both_goodRadio->setChecked(false);
        ui->right_goodRadio->setChecked(false);
    }
    else
    {
        ui->left_badRadio->setChecked(true);
        ui->both_badRadio->setChecked(false);
        ui->left_goodRadio->setChecked(false);
        ui->both_goodRadio->setChecked(false);
        ui->right_goodRadio->setChecked(true);
    }
}

void Details::on_birthday_dateEdit_dateChanged(const QDate &date)
{
    ui->age_label->setText(QString::number((QDate::currentDate().year()) - (date.year())));
}
